#include "mainwindow.h"
#include <QtDebug>
#include <QMessageBox>
#define LABELX0 20 //第一个边界
#define LABELX1 170 //第二个边界
#define LABELX2  320
#define LABELX3  470
//#define CARMAXNUM   10   //一次显示的汽车总数

MainWindow::MainWindow(QWidget *parent,void *p_map)
    : QMainWindow(parent)
{


     //字体设置
     QFont font("wenquanyi",12,QFont::Bold);
     this->setFont(font);
     
    //ui->setupUi(this);

    //公交信息显示全部采用label来表示，因为不需要用户的交互信息
     this->setCursor(Qt::BlankCursor);
     
         this->p_map  =(car_infos*) p_map; //获取共享内存指针
	 //	 qDebug("coming mainwindow init\n");
	 
           this->setWindowFlags( Qt::Window | Qt::FramelessWindowHint); //隐藏标题栏,使用无框架模式
	   
          this->resize(640, 480);

         centralWidget = new QWidget(this);
        centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
        QLabel *label = new QLabel(centralWidget);
        label->setObjectName(QString::fromUtf8("label"));
        label->setGeometry(QRect(0, 0, 640, 480));
        label->setPixmap(QPixmap(QString::fromUtf8("./carback.png")));
        this->setCentralWidget(centralWidget);
	centralWidget->setWindowState(centralWidget->windowState()^ Qt::WindowFullScreen);


	
    QLabel *labelCNum,*labelCPlace,*labelCTimeArri,*labelCTimeWait;
    //公交车次标签
    labelCNum = new QLabel(QObject::tr("<h3><font color = red>公交车次</font></h3>"), centralWidget);
    labelCNum->setGeometry(LABELX0,140,60,20);
    labelCNum->adjustSize();

    labelCPlace = new QLabel(QObject::tr("<h3><font color=green>现在所在站点</font</h3>"),centralWidget);
    labelCPlace->setGeometry(LABELX1,140,90,20);
    labelCPlace->adjustSize();

    labelCTimeArri = new QLabel(QObject::tr("<h3><font color=blue>预计到站时间</font></h3>"),centralWidget);
    labelCTimeArri->setGeometry(LABELX2,140,60,20);
    labelCTimeArri->adjustSize();

    labelCTimeWait = new QLabel(QObject::tr("<h3><font color=fuchsia>预计等待时间</font></h3>"),centralWidget);
    labelCTimeWait->setGeometry(LABELX3,140,60,20);
    labelCTimeWait->adjustSize();


    wday[0] = "星期天";
    wday[1] = "星期一";
    wday[2] = "星期二";
    wday[3] = "星期三";
    wday[4] = "星期四";
    wday[5] = "星期五";
    wday[6] = "星期六";
    time(&now);
    p = localtime(&now);
   //  char str_time[128];
    sprintf(str_time," %d-%d-%d %s  %02d:%02d:%02d" ,(1900+p->tm_year),(1 + p->tm_mon),p->tm_mday,wday[p->tm_wday],p->tm_hour,p->tm_min ,p->tm_sec );
    labelNowTime = new QLabel(QObject::tr(str_time),centralWidget);
    labelNowTime->setGeometry(200,5,60,20);
    labelNowTime->adjustSize();
    for(int i = 0; i< CARMAXNUM; i++)
    {
        QString msg = QString::number(i,10);
        CarInfos infos;
        infos.car_num =  new QLabel(msg,centralWidget);
        infos.car_num->setGeometry(LABELX0+20,160+i*30,30,30);
        infos.car_place=  new QLabel(msg,centralWidget);
        infos.car_place->setGeometry(LABELX1+20,160+i*30,30,30);
        infos.car_time_arriv= new QLabel(msg,centralWidget);
        infos.car_time_arriv->setGeometry(LABELX2+20,160+i*30,30,30);
        infos.car_time_wait=new QLabel(msg,centralWidget);
        infos.car_time_wait->setGeometry(LABELX3+20,160+i*30,30,30);
        vector_infos.append(infos);

    }

      startTimer(1000);//启动定时器1s相应一一次


}

MainWindow::~MainWindow()
{
   
}

void MainWindow::updateInfo(int *msg,int size)
{
       //更新标志中的显示数据
     int index;//保存数据的索引下标
     index = CarNumToSerial(msg[0]);
     //如果是-1没有本机不接受这个车号退出程序
     if(index == -1)
     {
	  return ;
	  
     }
     
        time(&now);
        p = localtime(&now);

        wait_hour = abs(p->tm_hour-msg[2]);
        wait_min  = abs(p->tm_min-msg[3]);

        //更新显示时间
       sprintf(str_time,"  %d-%d-%d %s %02d:%02d:%02d" ,(1900+p->tm_year),(1 + p->tm_mon),p->tm_mday,wday[p->tm_wday],p->tm_hour,p->tm_min ,p->tm_sec );
       labelNowTime->setText(QObject::tr(str_time));
       labelNowTime->setGeometry(200,5,60,20);
       labelNowTime->adjustSize();
       
        QString str_now_time = nowTime.toString();
        QString str_car_num = QString::number(msg[0],10);
        QString str_car_place = QString::number(msg[1],10);
        QString str_car_time_arriv ="";
        str_car_time_arriv.sprintf("%02d:%02d",msg[2],msg[3]);
        QString str_car_time_wait  ="";
       str_car_time_wait.sprintf("%02d:%02d",wait_hour,wait_min);
       // str_car_time_wait = QString(str_car_time_wait.toUtf8())
       vector_infos[index].car_num->setText(str_car_num);
       vector_infos[index].car_num->adjustSize();
       vector_infos[index].car_num->setAlignment(Qt::AlignCenter);

       // qDebug("coming  1 step\n");
       
       vector_infos[index].car_place->setText(str_car_place);
       vector_infos[index].car_place->adjustSize();
       vector_infos[index].car_place->setAlignment(Qt::AlignCenter);

//qDebug("coming 2 step\n");

        vector_infos[index].car_time_arriv->setText(str_car_time_arriv);
       vector_infos[index].car_time_arriv->adjustSize();
        vector_infos[index].car_time_arriv->setAlignment(Qt::AlignCenter);
//qDebug("coming 3 step\n");
       vector_infos[index].car_time_wait->setText(str_car_time_wait);
       vector_infos[index].car_time_wait->adjustSize();
       vector_infos[index].car_time_wait->setAlignment(Qt::AlignCenter);
       
       
}


void MainWindow:: timerEvent(QTimerEvent *event)
{
       int msg[4];
      for(int i = 0; i<CARMAXNUM; i++)
      {
                    memcpy(msg,p_map+i,4*4);
                   // qDebug()<<"map+i"<<p_map+i;
                    /*
                    memcpy(&msg[0],(*(p_map+i)).car_id,4);
                    memcpy(&msg[1],(*(p_map+i)).car_place,4);
                    memcpy(&msg[2],(*(p_map+i)).car_time_hour,4);
                    memcpy(&msg[3],(*(p_map+i)).car_time_min,4);
                    */
		    //  qDebug() << "time comming msg%d\n"<<msg[0]<<":"<<msg[1];
		     updateInfo(msg,4);
      }
      this->update(); //重绘

}


void MainWindow::keyPressEvent(QKeyEvent *event)
{
     //如果是退出键就退出程序
     if(event->key() == Qt::Key_Escape)
     {
	  
	  qApp->exit();
	  
	  
     }
     
     
}

int MainWindow::CarNumToSerial(int carnum)
{
     int i;
     for (i = 0; i<CARMAXNUM; i++)
     {
	  if(carnum == carNumToOrder[i])
	       return i;
	  
     }
     return -1;
     
     
}
